/* 
 * The MIT License
 *
 * Copyright 2014 Venkat Ram Akkineni.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.netbeans.modules.tfsnb.core.multiview;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Comparator;
import java.util.Map;
import javax.swing.DefaultButtonModel;
import javax.swing.JToggleButton;
import org.openide.util.ImageUtilities;

/**
 *
 * @author Venkat Ram Akkineni
 */
@Deprecated
public class SwitchViewToggleButton extends JToggleButton implements ActionListener {
        
    private final int position;
    private final MultiViewPanelDescription viewDesc;
    private final String id;

    public static final String BUTTON_ID_SUFFIX = "Button";
    private final MultiViewManager manager;
    
    public SwitchViewToggleButton(final MultiViewPanelDescription viewDesc, final String topComponentId, final MultiViewManager manager) {
        super(viewDesc.getDisplayName(), ImageUtilities.loadImageIcon(viewDesc.getIconBase(), true));
        this.position = viewDesc.getPosition();
        this.viewDesc = viewDesc;
        this.id = topComponentId + "_" +  viewDesc.getId() + "_" + BUTTON_ID_SUFFIX;
        this.manager = manager;
        this.setModel(new SwitchViewToggleButtonModel());
    }        

    public int getPosition() {
        return position;
    }

    public String getId() {
        return id;
    }

    public MultiViewManager getManager() {
        return manager;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        manager.switchPanel(id);
    }

    
    /**
     * The model handles the storage and maintenance of the state of the toggle button. This follows the same paradigm
     * (the MVC or Model-View-Controller design pattern) employed by other Swing components, where the data associated
     * with a component is stored separately from the display aspects.
     */
    public static class SwitchViewToggleButtonModel extends DefaultButtonModel {

        /**
         * Compatible with Sun's JDK.
         */
        private static final long serialVersionUID = -1589950750899943974L;

        /**
         * Sets the pressed state of the button. The selected state of the button also changes follwing the button being
         * pressed.
         *
         * @param p true if the button is pressed down.
         */
        public void setPressed(boolean p) {
            // cannot change PRESSED state unless button is enabled
            if (!isEnabled()) {
                return;
            }

            // if this call does not represent a CHANGE in state, then return
            if (isPressed()) {
                return;
            }

            // The JDK first fires events in the following order:
                 // 1. ChangeEvent for selected
                 // 2. ChangeEvent for pressed
                 // 3. ActionEvent
                 // So do we.
            // setPressed(false) == mouse release on us,
            // if we were armed, we flip the selected state.
            if (!p && isArmed()) {
                setSelected(!isSelected());
            }

            // make the change
            if (p) {
                stateMask = stateMask | PRESSED;
            } else {
                stateMask = stateMask & (~PRESSED);
            }

            // notify interested ChangeListeners
            fireStateChanged();

            if (!p && isArmed()) {
                fireActionPerformed(new ActionEvent(this,
                        ActionEvent.ACTION_PERFORMED,
                        actionCommand));
            }
        }
    }
    
    
    public static class ButtonComparator implements Comparator<String> {

        private final Map<String, SwitchViewToggleButton> baseMap;

        public ButtonComparator(Map<String, SwitchViewToggleButton> baseMap) {
            this.baseMap = baseMap;
        }

        @Override
        public int compare(String o1, String o2) {
            if (baseMap.get(o1).getPosition() < baseMap.get(o2).getPosition()) {
                return -1;
            } else {
                return 1;
            } // returning 0 would merge keys
        }

    }
            
}
